﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DoubleH.Plugins;
using Table = FCNS.Data.Table;
using System.Diagnostics;
using System.Collections.ObjectModel;
using DoubleH.Utility;
using DoubleH.Utility.Configuration;
using System.Net.Mail;
using System.Net;

namespace SysConfig
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        FCNS.Data.Table.SysConfig sysConfig = FCNS.Data.Table.SysConfig.SysConfigParams;
        ObservableCollection<Table.GroupS> groupS = new ObservableCollection<Table.GroupS>(Table.GroupS.GetList(Table.GroupS.EnumFlag.操作员分类, Table.GroupS.EnumEnable.启用));
        Int64 selectedGroupIndex = -1;
        IPluginHost ihost = null;

        public MainWindow()
        {
            InitializeComponent();

            InitVar();
            InitEvent();
        }

        public void Init(IPluginHost host)
        {
            ihost = host;
            Table.UserS userS = Table.UserS.LoginUser;
            if (userS.GetAuthority("数据库配置") < Table.UserS.EnumAuthority.编辑)
            {
                tabItemDatabase.Visibility = Visibility.Collapsed;
                tabItemModel.Visibility = Visibility.Collapsed;
            }
            else
            {
                InitDatabase(true);
                InitModel(true);
            }

            if (userS.GetAuthority(DataTableText.系统配置) < Table.UserS.EnumAuthority.编辑)
                tabItemSys.Visibility = Visibility.Collapsed;
            else
                InitSysConfig(true);

            if (userS.GetAuthority("用户配置") < Table.UserS.EnumAuthority.编辑)
                tabItemUser.Visibility = Visibility.Collapsed;
            else
                InitUserList();

            InitUserConfig(true);

            tabControlAll.SelectedItem = tabItemUI;
        }

        #region 配置
        private void InitDatabase(bool init)
        {
            if (init)
            {
                integerUpDownDecimalPlaces.Value = sysConfig.DecimalPlaces;
                integerUpDownDelayTime.Value = DoubleHConfig.AppConfig.DelayTime;
                integerUpDownSearchThreshold.Value = DoubleHConfig.AppConfig.SearchThreshold;
                doubleUpDownScore.Value = sysConfig.ScoreValue;

                checkBoxAutoAuditor.IsChecked = sysConfig.AutoAuditor;
                checkBoxAutoPost.IsChecked = sysConfig.AutoPost;
                checkBoxAutoMoney.IsChecked = sysConfig.AutoMoney;
                checkBoxProductRow.IsChecked = sysConfig.ProductRow;

                textBoxCompanyAddress.Text = sysConfig.CompanyAddress;
                textBoxCompanyEmail.Text = sysConfig.CompanyEmail;
                textBoxCompanyFax.Text = sysConfig.CompanyFax;
                textBoxCompanyName.Text = sysConfig.CompanyName;
                textBoxCompanyTel.Text = sysConfig.CompanyTel;
                textBoxCompanyWebSite.Text = sysConfig.CompanyWebSite;

                comboBoxEditMode.Items.Add(Table.SysConfig.EnumEditMode.权限分配);
                comboBoxEditMode.Items.Add(Table.SysConfig.EnumEditMode.用户组);
                comboBoxEditMode.Items.Add(Table.SysConfig.EnumEditMode.自己);
                comboBoxEditMode.SelectedIndex = (int)sysConfig.EditMode;
            }
            else
            {
                sysConfig.DecimalPlaces = integerUpDownDecimalPlaces.Value.Value;
                DoubleHConfig.AppConfig.DelayTime = integerUpDownDelayTime.Value.Value;
                DoubleHConfig.AppConfig.SearchThreshold = integerUpDownSearchThreshold.Value.Value;
                sysConfig.ScoreValue = doubleUpDownScore.Value.Value;

                sysConfig.AutoAuditor = checkBoxAutoAuditor.IsChecked.Value;
                sysConfig.AutoPost = checkBoxAutoPost.IsChecked.Value;
                sysConfig.AutoMoney = checkBoxAutoMoney.IsChecked.Value;
                sysConfig.ProductRow = checkBoxProductRow.IsChecked.Value;

                sysConfig.EditMode = comboBoxEditMode.SelectedIndex;

                sysConfig.CompanyAddress = textBoxCompanyAddress.Text;
                //sysConfig.CompanyEmail = textBoxCompanyEmail.Text;//在下面的验证处保存这个值
                sysConfig.CompanyFax = textBoxCompanyFax.Text;
                sysConfig.CompanyName = textBoxCompanyName.Text;
                sysConfig.CompanyTel = textBoxCompanyTel.Text;
                sysConfig.CompanyWebSite = textBoxCompanyWebSite.Text;
            }
        }

        private void InitModel(bool init)
        {
            if (init)
            {
                //模块
                checkListBoxModel.DisplayMemberPath = "ModelText";
                checkListBoxModel.SelectedMemberPath = "Used";
                IEnumerable<Table.SysConfig.Model> models = Table.SysConfig.SysConfigParams.AllModel();
                checkListBoxModel.ItemsSource = models;
                foreach (Table.SysConfig.Model m in models)
                    SetModel(m, m.Used);

                //采购
                checkBoxLastPurchasePrice.IsChecked = sysConfig.LastPurchasePrice;
                //批发
                checkBoxNegativeSales.IsChecked = sysConfig.NegativeSales;
                //零售
                checkBoxPosShenHe.IsChecked = sysConfig.PosShenHe;
                //仓库
                checkBoxLockQuantityWherePandian.IsChecked = sysConfig.LockQuantityWherePanDian;
                checkBoxCheckBarcode.IsChecked = sysConfig.CheckBarcode;
                //售后
                integerUpDownRequestTime.Value = (int)sysConfig.RequestTime;
                doubleUpDownKaoHeFenShu.Value = sysConfig.DefaultKaoHeFenShu;
                integerUpDownWeiBaoAlertDay.Value = (int)sysConfig.WeiBaoAlertDay;
                checkBoxBaoXiuHasSale.IsChecked = sysConfig.FanXiuHasSale;

                comboBoxMoneyAfterSales.Items.Add(Table.AfterSaleServiceS.EnumEnable.维护完工);
                comboBoxMoneyAfterSales.Items.Add(Table.AfterSaleServiceS.EnumEnable.回访反馈);
                comboBoxMoneyAfterSales.SelectedItem = sysConfig.CalcMoneyAfterSaleServiceS;
                //财务
                integerUpDownMoneyAlertDayFu.Value = (int)sysConfig.FuKuanAlertDay;
                integerUpDownMoneyAlertDayShou.Value = (int)sysConfig.ShouKuanAlertDay;
                //报表
                timePickerStart.Value = DateTime.ParseExact(DoubleHConfig.UserConfig.WorkStart, FCNS.Data.DbDefine.timeFormat, null);
                timePickerEnd.Value = DateTime.ParseExact(DoubleHConfig.UserConfig.WorkEnd, FCNS.Data.DbDefine.timeFormat, null);

                InitDatabaseParamEvent();
            }
            else
            {
                //采购
                sysConfig.LastPurchasePrice = checkBoxLastPurchasePrice.IsChecked.Value;
                //批发
                sysConfig.NegativeSales = checkBoxNegativeSales.IsChecked.Value;
                //零售
                sysConfig.PosShenHe = checkBoxPosShenHe.IsChecked.Value;
                //仓库
                sysConfig.LockQuantityWherePanDian = checkBoxLockQuantityWherePandian.IsChecked.Value;
                sysConfig.CheckBarcode = checkBoxCheckBarcode.IsChecked.Value;
                //售后
                sysConfig.RequestTime = integerUpDownRequestTime.Value.Value;
                sysConfig.DefaultKaoHeFenShu = doubleUpDownKaoHeFenShu.Value.Value;
                sysConfig.WeiBaoAlertDay = integerUpDownWeiBaoAlertDay.Value.Value;
                sysConfig.FanXiuHasSale = checkBoxBaoXiuHasSale.IsChecked.Value;
                sysConfig.CalcMoneyAfterSaleServiceS = (Table.AfterSaleServiceS.EnumEnable)comboBoxMoneyAfterSales.SelectedItem;
                //财务
                sysConfig.FuKuanAlertDay = integerUpDownMoneyAlertDayFu.Value.Value;
                sysConfig.ShouKuanAlertDay = integerUpDownMoneyAlertDayShou.Value.Value;
                //报表
                DoubleHConfig.UserConfig.WorkStart = timePickerStart.Value.Value.ToString(FCNS.Data.DbDefine.timeFormat);
                DoubleHConfig.UserConfig.WorkEnd = timePickerEnd.Value.Value.ToString(FCNS.Data.DbDefine.timeFormat);
            }
        }

        private void InitDatabaseParamEvent()
        {
            checkListBoxModel.ItemSelectionChanged += (ss, ee) => { SetModel(ee.Item as Table.SysConfig.Model, ee.IsSelected); };
            checkBoxSearchDataByUserConfig.Checked += (ss, ee) => { integerUpDownSearchDataByUserConfig.IsEnabled = checkBoxSearchDataByUserConfig.IsChecked.Value; };
            tabControlAll.SelectionChanged += (ss, e) => { buttonAuthority.IsEnabled = (tabControlAll.SelectedItem == tabItemUser); };
        }

        private void InitSysConfig(bool init)
        {
            if (init)
            {
                textBoxServerIP.Text = DoubleHConfig.AppConfig.ServerIP =
                    (FCNS.Data.SQLdata.SqlConfig.DataType == FCNS.Data.DataType.SQLITE ? "127.0.0.1" : FCNS.Data.SQLdata.SqlConfig.IPorName);

                integerUpDownServerPort.Value = DoubleHConfig.AppConfig.ServerPort;

                checkBoxAutoUpdate.IsChecked = DoubleHConfig.AppConfig.CheckUpdate;
            }
            else
            {

                DoubleHConfig.AppConfig.ServerIP = textBoxServerIP.Text;
                DoubleHConfig.AppConfig.ServerPort = integerUpDownServerPort.Value.Value;

                DoubleHConfig.AppConfig.CheckUpdate = checkBoxAutoUpdate.IsChecked.Value;
            }
        }

        private void InitUserConfig(bool init)
        {
            if (init)
            {
                checkBoxScheduleS.IsChecked = DoubleHConfig.UserConfig.ScheduleSUseImageForFlag;

                integerUpDownAlertTimeByUserConfig.Value = DoubleHConfig.UserConfig.AlertTime;

                checkBoxSearchDataByUserConfig.IsChecked = DoubleHConfig.UserConfig.GroupData;
                integerUpDownSearchDataByUserConfig.Value = (int)DoubleHConfig.UserConfig.GroupDataCount;
                checkBoxSynchronousData.IsChecked = DoubleHConfig.UserConfig.SynchronousData;

                textBoxSmtpAddress.Text = DoubleHConfig.UserConfig.EmailSmtp;
                textBoxEmailName.Text = DoubleHConfig.UserConfig.EmailName;
                passwordBoxEmailPwd.Password = DoubleHConfig.UserConfig.EmailPwd;
                checkBoxEmailSsl.IsChecked = DoubleHConfig.UserConfig.EmailSsl;
                integerUpDownPort.Value = DoubleHConfig.UserConfig.EmailPort;

                comboBoxUserProductPrice.SelectedIndex = DoubleHConfig.UserConfig.ProductPrice;

                InitExtension();
            }
            else
            {
                DoubleHConfig.UserConfig.ScheduleSUseImageForFlag = checkBoxScheduleS.IsChecked.Value;

                DoubleHConfig.UserConfig.AlertTime = integerUpDownAlertTimeByUserConfig.Value.Value;

                DoubleHConfig.UserConfig.GroupData = (bool)checkBoxSearchDataByUserConfig.IsChecked;
                DoubleHConfig.UserConfig.GroupDataCount = (uint)integerUpDownSearchDataByUserConfig.Value.Value;
                DoubleHConfig.UserConfig.SynchronousData = (bool)checkBoxSynchronousData.IsChecked;

                DoubleHConfig.UserConfig.EmailSmtp = textBoxSmtpAddress.Text;
                DoubleHConfig.UserConfig.EmailName = textBoxEmailName.Text;
                DoubleHConfig.UserConfig.EmailPwd = passwordBoxEmailPwd.Password;
                DoubleHConfig.UserConfig.EmailSsl = checkBoxEmailSsl.IsChecked.Value;
                DoubleHConfig.UserConfig.EmailPort = integerUpDownPort.Value.Value;

                DoubleHConfig.UserConfig.ProductPrice = comboBoxUserProductPrice.SelectedIndex;
            }
        }

        private void SetModel(Table.SysConfig.Model model, bool used)
        {
            Debug.Assert(model != null);
            //if (model.ModelText == Table.SysConfig.EnumModel.车辆租赁 && used)
            //{
            //    ListBoxItem item = (ListBoxItem)checkListBoxModel.FindName(Table.SysConfig.EnumModel.车辆管理.ToString());
            //    item.IsSelected = true;
            //}
            switch (model.ModelText)
            {
                case Table.SysConfig.EnumModel.采购进货: groupBoxPurchase.IsEnabled = used; break;
                case Table.SysConfig.EnumModel.仓库管理: groupBoxStore.IsEnabled = used; break;
                case Table.SysConfig.EnumModel.批发销售: groupBoxSales.IsEnabled = used; break;
                case Table.SysConfig.EnumModel.POS零售: groupBoxPos.IsEnabled = used; break;
                case Table.SysConfig.EnumModel.售后服务: groupBoxAfterSaleService.IsEnabled = used; break;
                case Table.SysConfig.EnumModel.车辆管理: break;
                case Table.SysConfig.EnumModel.报表中心: break;
            }
            //tabControlUser.Visibility = tabControlUser.HasItems ? Visibility.Visible : Visibility.Hidden;
        }

        private void InitContextMenu()
        {
            ContextMenu cmGroup = new ContextMenu();
            MenuItem mig1 = new MenuItem();
            mig1.Header = "新增用户组";
            mig1.Click += (ss, ee) => { NewUserGroup(); };
            cmGroup.Items.Add(mig1);
            MenuItem mig2 = new MenuItem();
            mig2.Header = "编辑用户组";
            mig2.Click += (ss, ee) => { EditUserGroup(); };
            cmGroup.Items.Add(mig2);
            MenuItem mig3 = new MenuItem();
            mig3.Header = "删除用户组";
            mig3.Click += (ss, ee) => { DeleteUserGroup(); };
            cmGroup.Items.Add(mig3);
            listBoxGroup.ContextMenu = cmGroup;

            ContextMenu cmUser = new ContextMenu();
            MenuItem miu1 = new MenuItem();
            miu1.Header = "新增用户";
            miu1.Click += (ss, ee) => { NewUser(); };
            cmUser.Items.Add(miu1);
            MenuItem miu2 = new MenuItem();
            miu2.Header = "编辑用户";
            miu2.Click += (ss, ee) => { EditUser(); };
            cmUser.Items.Add(miu2);
            //MenuItem miu3 = new MenuItem();
            //miu3.Header = "删除用户";
            //miu3.Click += (ss, ee) => { DeleteUser(); };
            //cmUser.Items.Add(miu3);
            listViewUser.ContextMenu = cmUser;
        }

        #endregion

        #region  操作员
        private void InitUserList()
        {
            listViewUser.DisplayMemberPath = "Name";
            listViewUser.SelectedValuePath = "Id";

            listBoxGroup.DisplayMemberPath = "Name";
            listBoxGroup.SelectedValuePath = "Id";
            listBoxGroup.ItemsSource = groupS;

            InitContextMenu();
            InitUserParamEvent();
        }

        private void InitUserParamEvent()
        {
            listBoxGroup.SelectionChanged += (ss, ee) =>
            {
                if (listBoxGroup.SelectedItem != null)
                {
                    selectedGroupIndex = (Int64)listBoxGroup.SelectedValue;
                    listViewUser.ItemsSource = Table.UserS.GetList(selectedGroupIndex, null);
                }
            };
            listBoxGroup.MouseDoubleClick += (ss, ee) => EditUserGroup();
            listViewUser.MouseDoubleClick += (ss, ee) => EditUser();
            listViewUser.SelectionChanged += (ss, ee) => InitUserAuthority();
            buttonAuthority.Click += (ss, ee) => SaveUserAuthority();
        }

        private void NewUserGroup()
        {
            GroupS.MainWindow mw = new GroupS.MainWindow();
            mw.Init(Table.GroupS.EnumFlag.操作员分类, null);
            mw.ShowDialog();
            groupS.Add(mw.NewGroupS);
        }

        private void EditUserGroup()
        {
            if (listBoxGroup.SelectedItem == null)
                return;

            GroupS.MainWindow mw = new GroupS.MainWindow();
            mw.Init(Table.GroupS.EnumFlag.操作员分类, listBoxGroup.SelectedItem as Table.GroupS);
            mw.ShowDialog();
        }

        private void DeleteUserGroup()
        {
            if (listBoxGroup.SelectedItem == null)
                return;

            Table.GroupS gs = (Table.GroupS)listBoxGroup.SelectedItem;
            Table.DataTableS.EnumDatabaseStatus result = gs.Delete();
            if (result == Table.DataTableS.EnumDatabaseStatus.操作成功)
                groupS.Remove(gs);
            else
                MessageWindow.Show(result.ToString());
        }

        private void NewUser()
        {
            WindowUser wu = new WindowUser();
            wu.Init(null);
            wu.ShowDialog();
            listViewUser.ItemsSource = Table.UserS.GetList(selectedGroupIndex, null);
        }

        private void EditUser()
        {
            if (listViewUser.SelectedItem == null)
                return;

            WindowUser wu = new WindowUser();
            wu.Init(listViewUser.SelectedItem as Table.UserS);
            wu.ShowDialog();
            listViewUser.ItemsSource = Table.UserS.GetList(selectedGroupIndex, null);
        }

        private void DeleteUser()
        {
            if (listViewUser.SelectedItem == null)
                return;

            MessageWindow.Show(((Table.UserS)listViewUser.SelectedItem).Delete().ToString());
            listViewUser.ItemsSource = Table.UserS.GetList(selectedGroupIndex, null);
        }

        private void InitUserAuthority()
        {
            if (listViewUser.SelectedItem == null)
                return;

            Table.UserS us = listViewUser.SelectedItem as Table.UserS;
            uCauthoritySystem.Items.Clear();
            uCauthorityReport.Items.Clear();

            uCauthoritySystem.Init(us,false,
                  DataTableText.系统配置,
        DataTableText.操作员,
        DataTableText.操作员分类,
        DataTableText.供应商,
        DataTableText.客户,
        DataTableText.会员资料,
        DataTableText.客商分类,
        DataTableText.会员分类,
        DataTableText.客户性质,
        DataTableText.区域,
        DataTableText.经办人分类,

       DataTableText.我的消息,
       DataTableText.待办事项,

         DataTableText.项目列表,
        DataTableText.任务跟踪,
       DataTableText.问题反馈,
       DataTableText.任务阶段名称,
       DataTableText.项目相关状态,

        DataTableText.仓库,
         DataTableText.仓库单据入库单,
        DataTableText.仓库单据出库单,
        DataTableText.仓库商品入库单,
        DataTableText.仓库商品出库单,
        DataTableText.非进货入库单,
        DataTableText.非销售出库单,
       DataTableText.调拨单,
       DataTableText.库存调价单,
       DataTableText.盘点单,

       DataTableText.采购询价单,
       DataTableText.采购订单,
       DataTableText.采购退货单,
       DataTableText.赠品入库单,
       DataTableText.自动补货,

        DataTableText.销售询价单,
        DataTableText.销售订单,
        DataTableText.销售退货单,
       DataTableText.赠品出库单,

       DataTableText.Pos客户端,
      DataTableText.Pos机器号,
       DataTableText.Pos禁售商品,

        DataTableText.产地,
       DataTableText.品牌,
       DataTableText.计量单位,
       DataTableText.仓库商品,
        DataTableText.商品分类,
       DataTableText.商品属性,
       DataTableText.可销售商品,
       DataTableText.可采购商品,
       DataTableText.物料清单,
     DataTableText.商品校验,
        DataTableText.条码打印,

        DataTableText.客户报修,
        DataTableText.售后派单,
        DataTableText.维护完工,
        DataTableText.回访反馈,
        DataTableText.员工评价,

        DataTableText.外包合同,
        DataTableText.服务级别定义,

        DataTableText.客户坏件送修,
       DataTableText.客户坏件取回,
        DataTableText.供应商坏件送修,
        DataTableText.供应商坏件返回,

         DataTableText.维修对象,
        DataTableText.维修类型,
        DataTableText.客户提供资料,
        DataTableText.服务评价,
       DataTableText.支付方式,
        DataTableText.费用类别,
        DataTableText.税率,

         DataTableText.收款单,
        DataTableText.付款单,
        DataTableText.其它费用支出单,
        DataTableText.其它费用收入单,
        DataTableText.未开具发票,
        DataTableText.未收取发票,
        DataTableText.可收支单据,

         DataTableText.车辆档案,
        DataTableText.预订车辆,
        DataTableText.行车记录,
        DataTableText.油票记录,

       DataTableText.违章处罚,
       DataTableText.事故赔偿,
       DataTableText.维修保养,
      DataTableText.年审记录,
      DataTableText.投保记录,

        DataTableText.物流公司,
        DataTableText.物流跟踪,
        DataTableText.物流查询);


            uCauthorityReport.Init(us,true,
                DataTableText.采购统计表,
       DataTableText.采购明细表,
       DataTableText.采购价格变动情况表,

        DataTableText.销售统计表,
        DataTableText.销售明细表,
       DataTableText.零售统计表,
        DataTableText.零售明细表,
        DataTableText.销售价格变动情况表,

        DataTableText.售后统计表,
       DataTableText.故障库,

        DataTableText.商品出入库明细表,
        DataTableText.商品实时库存表,

        DataTableText.账户余额表,
        DataTableText.收支明细表,
        DataTableText.应收支明细表,

       DataTableText.车辆成本,

        DataTableText.客商欠款表,
       DataTableText.商品毛利表,
        DataTableText.营业额);
        }

        private void SaveUserAuthority()
        {
            if (listViewUser.SelectedItem == null)
                return;

            Table.UserS us = (Table.UserS)listViewUser.SelectedItem;
            us.Authority.Clear();
            foreach (UC.UCauthority.Authority au in uCauthoritySystem.Items)
                us.Authority.Add(au.DataTableText, au.EA);
            foreach (UC.UCauthority.Authority au in uCauthorityReport.Items)
                us.Authority.Add(au.DataTableText, au.EA);

            MessageWindow.Show(us.UpdateAuthority().ToString());
        }
        #endregion

        private void InitVar()
        {
            comboBoxScheduleSFlag.ItemsSource = Enum.GetNames(typeof(Table.ScheduleS.EnumFlag));
        }

        private void InitEvent()
        {
            comboBoxScheduleSFlag.SelectionChanged += (ss, ee) =>
            {
                switch (comboBoxScheduleSFlag.SelectedIndex)
                {
                    case 0: buttonScheduleSImage.Content = string.IsNullOrEmpty(DoubleHConfig.UserConfig.ScheduleImage0) ? "选择图片" : DoubleHConfig.UserConfig.ScheduleImage0; break;
                    case 1: buttonScheduleSImage.Content = string.IsNullOrEmpty(DoubleHConfig.UserConfig.ScheduleImage1) ? "选择图片" : DoubleHConfig.UserConfig.ScheduleImage1; break;
                    case 2: buttonScheduleSImage.Content = string.IsNullOrEmpty(DoubleHConfig.UserConfig.ScheduleImage2) ? "选择图片" : DoubleHConfig.UserConfig.ScheduleImage2; break;
                    case 3: buttonScheduleSImage.Content = string.IsNullOrEmpty(DoubleHConfig.UserConfig.ScheduleImage3) ? "选择图片" : DoubleHConfig.UserConfig.ScheduleImage3; break;
                }
            };

            buttonScheduleSImage.Click += (ss, ee) =>
            {
                buttonScheduleSImage.Content = DoubleH.Utility.IO.FileTools.GetFile(true,".jpg", ".bmp", ".gif", ".png");
                switch (comboBoxScheduleSFlag.SelectedIndex)
                {
                    case 0: DoubleHConfig.UserConfig.ScheduleImage0 = buttonScheduleSImage.Content.ToString(); break;
                    case 1: DoubleHConfig.UserConfig.ScheduleImage1 = buttonScheduleSImage.Content.ToString(); break;
                    case 2: DoubleHConfig.UserConfig.ScheduleImage2 = buttonScheduleSImage.Content.ToString(); break;
                    case 3: DoubleHConfig.UserConfig.ScheduleImage3 = buttonScheduleSImage.Content.ToString(); break;
                }
            };

            buttonSave.Click += (ss, ee) =>
                {
                    if (Table.UserS.LoginUser.GetAuthority("数据库配置") > Table.UserS.EnumAuthority.查看 && (string.IsNullOrEmpty(textBoxCompanyEmail.Text) || !textBoxCompanyEmail.Text.Contains("@") || !textBoxCompanyEmail.Text.Contains(".")))
                    {
                        MessageWindow.Show("电子邮箱不能为空");
                        return;
                    }
                    if (textBoxCompanyEmail.Text != sysConfig.CompanyEmail || textBoxEmailName.Text != DoubleHConfig.UserConfig.EmailName)
                    {
                        sysConfig.CompanyEmail = textBoxCompanyEmail.Text;
                        textBoxCompanyEmail.Clear();
                    }

                    Table.UserS userS = Table.UserS.LoginUser;
                    if (userS.GetAuthority("数据库配置") > Table.UserS.EnumAuthority.查看)
                    {
                        InitDatabase(false);
                        InitModel(false);
                        sysConfig.Update();
                    }
                    if (userS.GetAuthority(DataTableText.系统配置) > Table.UserS.EnumAuthority.查看)
                    {
                        InitSysConfig(false);
                        ConfigSerializer.SaveConfig(DoubleHConfig.AppConfig, FCNS.Data.DbDefine.appConfigFile);
                    }
                    //if (userS.Authority.UserParam)
                    //{
                    //    InitUserList(false);
                    //    ConfigSerializer.SaveConfig(DoubleHConfig.UserConfig,userS.Id);
                    //}
                    InitUserConfig(false);
                    if (string.IsNullOrEmpty(textBoxCompanyEmail.Text))
                    {
                        FCNS.Data.Utility u = new FCNS.Data.Utility();
                        Action<string> action = u.SendMail;
                        action.BeginInvoke(textBoxEmailName.Text, null, null);
                    }

                    MessageWindow.Show("保存成功");
                    this.Close();
                };
        }

        private void InitExtension()
        {
            dataGridExtExtension.ItemsSource = ihost.ExtentionManager.Plugins;

            dataGridExtExtension.SelectionChanged += (ss, ee) =>
            {
                if (dataGridExtExtension.SelectedItem == null)
                    return;

                DoubleH.Plugins.PluginInfo info = dataGridExtExtension.SelectedItem as DoubleH.Plugins.PluginInfo;
                textBlockExtension.Text = info.Description;
            };
            labelExtension.MouseDown += (ss, ee) => { System.Diagnostics.Process.Start("http://www.fcnsoft.com/extension/default.html"); };
        }

    }
}